package states;

import interfaces.Controller;
import interfaces.StateController;
import brickBreaker.*;
import globals.*;

import java.util.ArrayList;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import buttons.ClickButton;
import buttons.ToggleButton;

public class BrickBreaker extends BasicGameState {
	// Internal State Memory ///
	// Game Objects
	Paddle pad1;
	Ball ball1;
	BrickArray bricks;
	
	// Support Objects
	BreakerPause pause;
	Controller controller;
	StateController sController;
	DataTracker ball1Track;
	
	// Structure Objects
	public Input input; // key imput
	ClickButton buttonSpeedup, buttonSpeeddown, buttonResetArray;
	ToggleButton buttonTogglePause;
	
	private int id;
	BrickBreaker(int stateId){ // Constructor
		this.id=stateId;
		//Initalize Internal Memory
		pad1=new Paddle(100, SlickGame.height-50);
		ball1=new Ball(SlickGame.width/2,SlickGame.height/2,4);
		bricks=new BrickArray(20,20,SlickGame.width-20,(int) (SlickGame.height/2.2), (float) 0.3);
		
		pause=new BreakerPause();
		controller=new Controller();
		sController=new StateController();	sController.setDelay(3*SlickGame.fps);
		ball1Track=new DataTracker(20,SlickGame.height/2,"Ball1 Data","X","Y","Vel",false);
		buttonSpeedup=new ClickButton(100,100,30,20,"+",Color.black,Color.red.darker(),Color.gray.darker(),Color.gray);
		buttonSpeeddown=new ClickButton(140,100,30,20,"-",Color.black,Color.red.darker(),Color.gray.darker(),Color.gray);
		buttonResetArray=new ClickButton(100,140,30,20,"0",Color.black,Color.red.darker(),Color.gray.darker(),Color.gray);
		buttonTogglePause=new ToggleButton(140,140,30,20,"p",SlickGame.fps*2,Color.black,Color.red.darker(),Color.gray.darker(),Color.gray);
}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {// Initalize Local Graphics/Sound
		pause.init(gc, sbg);
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		//g.setFont(GlobalResources.getFont("FontNormal"));
		g.setBackground(Color.black);
		
		
		ball1.draw(g);
		pad1.draw(g);
		bricks.draw(g);
		
		ball1Track.draw(g);
		buttonSpeedup.draw(g);
		buttonSpeeddown.draw(g);
		buttonResetArray.draw(g);
		buttonTogglePause.draw(g);
		g.setColor(Color.white);
		g.drawString("X: "+Mouse.getX()+":"+SlickGame.width+" Y: "+(SlickGame.height-Mouse.getY())+":"+SlickGame.height+" C: "+Mouse.isButtonDown(0), 10, 30);
		g.setColor(Color.gray.darker());
		g.drawString("BrickBreaker",SlickGame.width/2-20,SlickGame.height-20);
		
		if(pause.isPaused){
			pause.draw(g);
		}
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		this.input = gc.getInput();
		
		buttonTogglePause.update(Mouse.getX(), (SlickGame.height-Mouse.getY()), Mouse.isButtonDown(0), delta);
		if(buttonTogglePause.clicked){pause.togglePause(); buttonTogglePause.clicked=false;}
		
		sController.update(input, delta);
		pause.isPaused=sController.key_lShift;
		
		if(!pause.isPaused){
			buttonSpeedup.update(Mouse.getX(), (SlickGame.height-Mouse.getY()), Mouse.isButtonDown(0), delta);
			buttonSpeeddown.update(Mouse.getX(), (SlickGame.height-Mouse.getY()), Mouse.isButtonDown(0), delta);
			buttonResetArray.update(Mouse.getX(), (SlickGame.height-Mouse.getY()), Mouse.isButtonDown(0), delta);
			
			if(buttonSpeedup.clicked){ball1.direction.setMagnitude(ball1.direction.getMagnatude()+1);}
			if(buttonSpeeddown.clicked && ball1.direction.getMagnatude()>1){ball1.direction.setMagnitude(ball1.direction.getMagnatude()-1);}
			if(buttonResetArray.clicked){bricks.reset();}
			
			
			//controller.updateStandard(input, pad1);
			controller.updateAutoTrack(pad1, ball1,(float) 0.05);
			pad1.update(ball1);
			ball1.update(ball1Track);
			bricks.update(ball1);
			if(ball1.direction.getMagnatude()>15){ball1.direction.setMagnitude(19);}
		}
		else{pause.update(sbg,delta);}
	}
	
	public static void destroy(){
		
	}

	public int getID() {
		return this.id;
	}
}
